#!/usr/bin/env python
# encoding=utf-8

from kartograph import Kartograph
from xml.dom import minidom
import json

CONFIG_FILE = 'world-with-names.json'
OUTPUT_FILE = 'countries.json'


def createCountriesDict():
    """ Creates JSON file with basic information about each country
    """
    kartograph = Kartograph()
    with open(CONFIG_FILE) as f:
        config = json.load(f)
    svgString = unicode(kartograph.generate(config, preview=False))
    svgDoc = minidom.parseString(svgString.encode('utf-8'))
    #print svgDoc.toprettyxml()[:2000]
    countriesDict = {
        path.getAttribute('data-id'): {
            'name': path.getAttribute('data-name'),
            'population': int(float(path.getAttribute('data-population')))}
        for path in svgDoc.getElementsByTagName('path')}
    with open(OUTPUT_FILE, 'w') as f:
        f.write(json.dumps(countriesDict, indent=1))


if __name__ == "__main__":
    createCountriesDict()
